package StackandQueue;

import org.junit.Assert;
import org.junit.Test;

import java.util.Stack;

public class RemoveDuplicates {
    public static void main(String[] args) {
        String s = "abbaca";
        RemoveDuplicates solution = new RemoveDuplicates();
        System.out.println(solution.removeDuplicates(s));

    }

    public String removeDuplicates(String s){
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++){
            if(stack.isEmpty() || s.charAt(i) != stack.peek()){
                stack.push(s.charAt(i));
            }else {
                stack.pop();
            }
        }
        String str = "";
        while(!stack.isEmpty()){
            str = stack.pop() + str;
        }
        return str;
    }
}
